package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.entity.religion.Religion;
import com.tongzhan.entity.religion.Site;
import com.tongzhan.entity.religion.Street;
import com.tongzhan.entity.religion.Village;
import com.tongzhan.repository.religion.StreetRepository;
import com.tongzhan.repository.religion.VillageRepository;
import com.tongzhan.vo.StreetVO;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class StreetService {

    @Autowired
    private StreetRepository streetRepository;
    @Autowired
    private VillageRepository villageRepository;

    /**
     * 获取街道数据
     *
     * @return
     */
    public List<StreetVO> getStreetList() {
        //查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("StreetCode").nin("00"));
        query.with(Sort.by(Sort.Direction.ASC, "StreetCode"));
        //街道列表
        List<Street> streetList = streetRepository.findAll(query);
        //封装返回数据
        List<StreetVO> streetVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(streetList)) {
            for (Street street : streetList) {
                StreetVO streetVO = new StreetVO();
                streetVO.setStreetCode(street.getStreetCode());
                streetVO.setStreetName(street.getStreetName());
                streetVOS.add(streetVO);
            }
        }
        return streetVOS;
    }

    public List<Village> getVillageList(String streetCode) {
        List<Village> villages = new ArrayList<>();
        if (Strings.isNotBlank(streetCode)) {
            villages = villageRepository.findAll(new Query().addCriteria(Criteria.where("StreetCode").is(streetCode)));
        } else {
            villages = villageRepository.findAll();
        }
        return villages;
    }
}
